﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Finans
{
    public partial class AkreditifliProforma : XForm
    {
        public AkreditifliProforma()
        {
            InitializeComponent();
        }

        BindingSource bsAP;
        private void AkreditifliProforma_Load(object sender, EventArgs e)
        {
            TAM.AkreditifliProformaTableAdapter = new Datasets.MyDataTableAdapters.AkreditifliProformaTableAdapter();
            bsAP = new BindingSource(ds, "AkreditifliProforma");
            InitGridView(gvAP, bsAP);
            //gvAP.OptionsBehavior.Editable = false;
            gvAP.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gvAP.Columns["UserId"].Visible = gvAP.Columns["ModifiedOn"].Visible = true;
            //gvAP.OptionsView.ShowViewCaption = true;

            AddBsToList(bsAP);
            gvAP.ShowingEditor += new System.ComponentModel.CancelEventHandler(gvAP_ShowingEditor);
            gvAP.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvAP_RowStyle);

            foreach (string str in "IhracatciUnvani;FaturaAdresiVeUnvani;TeslimAdresiVeUnvani;DigerSartlar".Split(';'))
                gvAP.Columns[str].ColumnEdit = new RepositoryItemMemoEdit();

            gvAP.Columns["TeslimAdresiAyni"].ColumnEdit = new RepositoryItemCheckEdit();
            gvAP.RowHeight = 45;

            GetDetails("AkreditifliProforma", "GetAkreditifliProforma", WhoAmI);
            gvAP.BestFitColumns();

            ds.AkreditifliProforma.TableNewRow += new System.Data.DataTableNewRowEventHandler(AkreditifliProforma_TableNewRow);
            btnYeni.ItemClick += btnYeni_ItemClick;
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            // displaying an image within the header 
            ImageList images = new ImageList();
            images.Images.Add(ConfExpert.Properties.Resources.Action_Sorting_Ascending);
            //Image.FromFile("e:\\Images\\Icons\\detailsImage.bmp"));
            vgc.ImageList = images;
            CreateVerticalGrid();
            vgc.DataSource = bsAP;

            vgc.ShowingEditor += new System.ComponentModel.CancelEventHandler(vgc_ShowingEditor);

            gvAP.DoubleClick += new EventHandler(gvAP_DoubleClick);

        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
                throw new NotImplementedException();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Save();
            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);
        }

        void btnYeni_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bsAP.EndEdit();
            bsAP.AddNew();  //TODO: stackoverflow exception veriyor!
            bsAP.MoveLast();
            ExpandCollapse(splitContainerControl1, AcKapa.Expand);
            vgc.BestFit();
            //vgc.FocusedRow = vgc.Rows.GetRowByFieldName("APId", true);
        }

        void gvAP_DoubleClick(object sender, EventArgs e)
        {
            //xtraTabControl1.SelectedTabPage = tpYeni;
            ExpandCollapse(splitContainerControl1, AcKapa.Expand);
            vgc.BestFit();
            //vgc.FocusedRow = vgc.Rows.GetRowByFieldName("APId", true);
        }

        void vgc_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = vgc.FocusedRow.Name == "APId";
        }


        void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            //btnYeni.Enabled = (e.Page == tpYeni);
        }

        void gvAP_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (gvAP.GetRowCellValue(e.RowHandle, "stAPId") != null)
                if (intParse(gvAP.GetRowCellValue(e.RowHandle, "stAPId")) > 1)
                {
                    //e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Strikeout);
                    e.Appearance.ForeColor = Color.Crimson; // SystemColors.ControlDark;
                }
        }

        void AkreditifliProforma_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e)
        {
            e.Row["UserId"] = WhoAmI;
            e.Row["stAPId"] = 1;
        }

        void gvAP_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !ytk(174) && (intParse(gvAP.GetFocusedRowCellValue("stAPId")) > 1 
                || gvAP.FocusedColumn.FieldName == "stAPId");
        }


        private void CreateVerticalGrid()
        {
            AddVerticalGridGroup(vgc, "Temel", new BaseRow[] {
                AddItem("APId", "APId"),
                AddItem("Durum", "stAPId", RI(Rp.stAP)),
                AddItem("İhracatçı Ünvanı", "IhracatciUnvani"),
                AddItem("Fatura Adresi ve Ünvanı", "FaturaAdresiVeUnvani", new RepositoryItemMemoEdit()),
                AddItem("Teslim Adresi Aynı", "TeslimAdresiAyni", new RepositoryItemCheckEdit()),
                AddItem("Teslim Adresi ve Ünvanı", "TeslimAdresiVeUnvani", new RepositoryItemMemoEdit())
            });

            AddVerticalGridGroup(vgc, "Teknik", new BaseRow[] {
                AddItem("Ödeme Süresi Gün", "apOdemeSuresiGunId", RI(Rp.apOdemeSuresiGun)),
                AddItem("Son Yukleme Tarihi", "SonYuklemeTarihi"),
                AddItem("Yükleme Şekli", "apYuklemeSekliId", RI(Rp.apYuklemeSekli)),
                AddItem("Navlun Şekli", "apNavlunSekliId", RI(Rp.apNavlunSekli)),
                AddItem("Teslim Şekli", "apTeslimSekliId", RI(Rp.TeslimSekliA)),
                AddItem("Akreditif Türü", "apAkreditifTuruId", RI(Rp.apAkreditifTuru)),
                AddItem("Tolerans Oranı %", "ToleransOraniYuzde")

            });

            AddVerticalGridGroup(vgc, "Notlar", new BaseRow[] {
                AddItem("Diğer Şartlar", "DigerSartlar", new RepositoryItemMemoEdit()),
                AddItem("Talepte Bulunan", "UserId", RI(Rp.V_User)),
                AddItem("Tarih", "ModifiedOn")
            });

            //AddGroup("Reorder", new MultiEditorRow[] {
            //    AddMultiItem("Point", "ReorderPoint",
            //        "Qty", "ReorderQty")
            //});

            //vgc.Rows.GetRowByFieldName("DN", true).Properties.ReadOnly = true;
            //vgc.Rows.GetRowByFieldName("TS", true).Visible = false;
            foreach(string str in "FaturaAdresiVeUnvani;TeslimAdresiVeUnvani".Split(';'))
                vgc.Rows.GetRowByFieldName(str, true).Height = 60;

            vgc.Rows.GetRowByFieldName("DigerSartlar", true).Height = 90;

            //foreach (string Message in "IhracatciUnvani;SonYuklemeTarihi".Split(';'))
            //    vgc.Rows.GetRowByFieldName(Message, true).Appearance.BackColor = Color.FromName(AppSession.Settings.ZorunluAlanRengi);

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;
        }

        private BaseRow AddItem(string caption)
        {
            return AddVerticalGridItem(vgc, caption, caption);
        }

        private BaseRow AddItem(string caption, string name, params RepositoryItem[] ri)
        {
            return AddVerticalGridItem(vgc, caption, name, ri);
        }


    }
}